//{==================================================================================================
//! @file StudentsCount.cpp
//! @date 2013-09-29
//! @mainpage Program will use rhyme to students until three remain
//! @author Kirill Shcherbatov <kshcherbatov@gmail.com>, 376 group, DCAM MIPT
//! @version 1.0
//}===================================================================================================

//****************************************************************************************************
#include <stdio.h>
#include <stdlib.h>
//****************************************************************************************************
//****************************************************************************************************
#define _EJC
#define PARANOID_MODE
//****************************************************************************************************

//****************************************************************************************************
//! @def   OUT
//! @note  -Use OUT to print your text in console. It is checking _EJC declaration.
//! @note  -Only if _EJC is defined then the program prints your text.
//! @note  --You also can define _EJC in compilation command (g++ main.cpp -D _EJC)
//----------------------------------------------------------------------------------------------------
#ifdef _EJC
    #define OUT printf
#else
    #define OUT if (0) printf
#endif // _EJC
//****************************************************************************************************


//****************************************************************************************************
//! @def    VALID
//! @param  CONDITIONAL for checking
//! @param  MESSAGE for displaying if CONDITIONAL is false
//! @note   -It checks the validity of the expression.
//! @note   -Program will use OUT to display MESSAGE
//! @note   -If sended conditional is false, program will be terminated sending 1 message
//----------------------------------------------------------------------------------------------------
#ifdef  PARANOID_MODE
    #define VALID( CONDITIONAL, MESSAGE )       \
    if (!(CONDITIONAL))                         \
    {                                           \
        OUT  ( MESSAGE );                       \
        exit ( 1 );                             \
    } // VALID in PARANOID_MODE
#else
    #define VALID( CONDITIONAL, MESSAGE ) \
    (void) CONDITIONAL;
#endif
//****************************************************************************************************

//****************************************************************************************************
//! @def    CHECK
//! @param  CONDITIONAL for checking
//! @param  MESSAGE for displaying if CONDITIONAL is false
//! @note   -It checks the validity of the expression.
//! @note   -Program will use OUT to display MESSAGE
//! @note   -If sended conditional is false, program will be terminated sending 1 message
//! @warning   IN RELIASE MODE IT WILL BE REMOOVED
//----------------------------------------------------------------------------------------------------
#ifdef PARANOID_MODE
    #define CHECK( CONDITIONAL, MESSAGE )       \
    if (!(CONDITIONAL))                         \
    {                                           \
        OUT  ( MESSAGE );                       \
        exit ( 1 );                            \
    } // CHECK in PARANOID_MODE
#else
    #define CHECK( CONDITIONAL, MESSAGE )
#endif
//****************************************************************************************************



//****************************************************************************************************
//! @param - number of sending arguments
//! @param argv[] - arguments, include generated by OS
//! @return 0 on success
//----------------------------------------------------------------------------------------------------
int main(int, const char* argv[])
{
    OUT ("%s (%s) %s %s:\n\n", argv[ 0 ], __FILE__, __DATE__, __TIME__ );
    OUT ("#Program will use rhyme to students until three remain\n");

    const unsigned int SUPRENUM = 10000;

    OUT ( "#Enter integer n, the number of students.\n"
           "     #It should be natural integer from (0; %d)\n"
        "#n = ", SUPRENUM);

    unsigned int students_n=0;

    VALID ( scanf("%d",&students_n) == 1 , "#Problems with inputed n. Read the instructions above & try again.\n" );
    CHECK ( students_n > 0 ,  "#Inputed N is incorrect. N should be greater than 0.\n" );
    CHECK ( students_n < SUPRENUM , "#Inputed N is incorrect. N should be lower than 10 000.\n");

    OUT ( "#Enter integer m, the number of words in rhyme.\n"
          "     #It should be natural integer from (0; %d)\n"
          "#m = ", 3*SUPRENUM);

    char * students = (char *)calloc(students_n, sizeof(char));
    for (unsigned int i=0; i<students_n; i++)  students[i]=1;

    unsigned int words_n=0;

    VALID ( scanf("%d",&words_n) == 1 , "#Problems with inputed n. Read the instructions above & try again.\n" );
    CHECK ( words_n > 0 ,  "#Inputed N is incorrect. N should be greater than 0.\n" );
    CHECK ( words_n < SUPRENUM , "#Inputed N is incorrect. N should be lower than SUPRENUM.\n");

    unsigned int choose=0, pos=0;

    while (choose < students_n-3)
    {
        unsigned int already_counted=0;
        while (already_counted < words_n)
        {
            while ((students[pos] == 0) && (pos < students_n)) pos++;
            if (pos >= students_n)
            {
                pos=0;
                while ((students[pos] == 0) && (pos < students_n)) pos++;
            }
            already_counted++; pos++;
        }
        students[pos-1]=0;
        choose++;
    }

    OUT ("#Survived 3 students with numbers\n");

    choose=0;

    for (pos=0; (pos<students_n && choose<4); pos++)
        if (students[pos]!=0)
        {
            printf("%d ",pos+1);
            choose++;
        }

    OUT ("\n");

    free(students);
    students=NULL;

    return 0;
}
//****************************************************************************************************
